﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using Cvk.News;

namespace BatDongSan.Manager
{
    public partial class ManNewsUpdate : System.Web.UI.Page
    {
        public static int _id, _idType;
        protected void Page_Load(object sender, EventArgs e)
        {
            _idType = Convert.ToInt32(Request.QueryString["idt"]);
            _id = Convert.ToInt32(Request.QueryString["id"]);
            if (!IsPostBack)
            {
                BindNewsType();
                if (_idType > 0)
                {
                    ddlNewsType.SelectedValue = Convert.ToString(_idType);
                }
                if (_id > 0)
                    LoadInfo(_id);
                else { btnUpdate.Text = "Thêm mới"; FocusDateNow(); }

            }
        }
        protected void BindNewsType()
        {
            try
            {
                List<VG_NewsType> list = NewsBll.claNewsTypeDetail.GetListParent(1, Utility.strConnect);
                if (list.Count > 0)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        VG_NewsType module = list[i];

                        if (list[i].ParentId == 0) //Nếu idparent=0 thì in Hoa tòan bộ chữ
                        {
                            ListItem li = new ListItem(list[i].TypeName.ToUpper(), list[i].NewsTypeId.ToString());
                            li.Attributes.Add("style", "color:red");
                            ddlNewsType.Items.Add(li);
                            //ddlNewsType.Items.Add(new ListItem(list[i].TypeName.ToUpper(), list[i].NewsTypeId.ToString()));
                            List<VG_NewsType> list_sub = NewsBll.claNewsTypeDetail.GetListByParentId(1, list[i].NewsTypeId, Utility.strConnect);
                            if (list_sub.Count > 0)
                            {
                                for (int j = 0; j < list_sub.Count; j++)
                                {
                                    ddlNewsType.Items.Add(new ListItem("--" + list_sub[j].TypeName, list_sub[j].NewsTypeId.ToString()));
                                }
                            }
                        }
                    }
                }
            }
            catch { }
        }
        private void FocusDateNow()
        {
            try
            {
                DateTime dt = DateTime.Now;
                ddlhour.SelectedIndex = dt.Hour;
                ddlminute.SelectedIndex = dt.Minute;
                ddlsecon.SelectedIndex = dt.Second;
                txtdate.Text = dt.ToString("MM/dd/yyyy");
            }
            catch { }
        }
        public void LoadInfo(int id)
        {
            try
            {
                //Load News
                Cvk_New infoNew = new Cvk_New();
                infoNew = NewsBll.claNews.GetNews(id, Utility.strConnect);
                if (infoNew != null)
                {
                    HiddenField1.Value = infoNew.ImageThumb;
                    HiddenField2.Value = infoNew.ImageSlide;
                    ckbActive.Checked = Convert.ToBoolean(infoNew.IsActive);
                    ckbHoted.Checked = Convert.ToBoolean(infoNew.IsHoted);
                    ckbSlided.Checked = Convert.ToBoolean(infoNew.IsSlided);
                    txtLinks.Text = infoNew.Links;
                    txtAuthor.Text = infoNew.Author;
                    DateTime dt = Convert.ToDateTime(infoNew.PostDate);
                    ddlhour.SelectedIndex = dt.Hour;
                    ddlminute.SelectedIndex = dt.Minute;
                    ddlsecon.SelectedIndex = dt.Second;
                    txtdate.Text = dt.ToString("MM/dd/yyyy");

                }
                //Load Vietnammes
                Cvk_NewsDetail info = new Cvk_NewsDetail();
                info = NewsBll.claNewsDetail.GetInfo(1, id, Utility.strConnect);
                if (info != null)
                {
                    txtSubject.Text = info.Subject;
                    txtNote.Text = info.Note;
                    fckContent.Value = info.Content;
                    txtSEOTitle.Text = info.SeoTitle;
                    txtSEODescription.Text = info.SeoDescription;
                    txtSEOKeyword.Text = info.SeoKeyword;
                    //txtSEOImageAlt.Text = info.SeoImageAlt;
                    //txtSEOImageTitle.Text = info.SeoImageTitle;
                    txtSEOUrl.Text = info.SeoUrl;
                }
            }
            catch { }
        }
        protected void lkb_key_Click(object sender, EventArgs e)
        {
            try
            {
                string text_exist = txtSEOKeyword.Text;
                if (!string.IsNullOrEmpty(text_exist))
                {
                    text_exist = text_exist + ", ";
                }
                LinkButton lkb = (LinkButton)sender;
                txtSEOKeyword.Text = text_exist + lkb.Text;
                txtSEOKeyword.Focus();
            }
            catch { }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                #region Check Information
                if (string.IsNullOrEmpty(txtSubject.Text))
                {
                    lbStatus.Text = "Vui lòng nhập đầy đủ thông tin cần thiết.";
                    txtSubject.Focus();
                    return;
                }
                #endregion

                #region Set Information
                Cvk_NewsDetail info = new Cvk_NewsDetail();
                info.NewsId = _id;
                info.CultureId = 1;
                info.Subject = txtSubject.Text;
                info.Note = txtNote.Text;
                info.Content = fckContent.Value;
                info.SeoDescription = txtSEODescription.Text;
                info.SeoKeyword = txtSEOKeyword.Text;
                info.SeoTitle = txtSEOTitle.Text;
                //info.SeoImageAlt = txtSEOImageAlt.Text;
                //info.SeoImageTitle = txtSEOImageTitle.Text;
                info.SeoUrl = txtSEOUrl.Text;

                Cvk_New infoNew = new Cvk_New();
                infoNew.NewsId = _id;
                infoNew.IsSlided = ckbSlided.Checked;
                infoNew.PostDate = Convert.ToDateTime(txtdate.Text + " " + ddlhour.SelectedValue + ":" + ddlminute.SelectedValue + ":" + ddlsecon.SelectedValue);
                infoNew.UpdateDate = DateTime.Now;
                infoNew.IsDeteted = false;
                infoNew.IsActive = ckbActive.Checked;
                infoNew.IsHoted = ckbHoted.Checked;
                infoNew.Links = txtLinks.Text;
                infoNew.BrowseUser = Request.Cookies["mrvuManager"]["Username"].ToString();
                infoNew.PostUser = "NotMember";
                infoNew.Author = txtAuthor.Text;
                infoNew.Views = 0;

                #region Upload Image Thumb
                string fileName1 = string.Empty;
                string filePath1 = Server.MapPath(string.Format("~/Uploads/News/"));
                try
                {
                    if (!Directory.Exists(filePath1))
                    {
                        Directory.CreateDirectory(filePath1);
                    }
                    if (FileUpload1.HasFile)
                    {
                        if (cvk.AppCode.WebUtility.IsValidFile(FileUpload1.FileName, ".jpg", ".jpeg", ".gif", ".JPG", ".JPEG", ".GIF", ".bmp", ".Bmp", ".BMP") && FileUpload1.FileContent.Length <= 3145728) //3M
                        {
                            fileName1 = cvk.AppCode.WebUtility.GetDateTimeString(FileUpload1.FileName);
                            cvk.AppCode.WebUtility.UploadImage(FileUpload1, filePath1, fileName1, 150, 118);
                            //FileUpload1.SaveAs(filePath1 + fileName1);
                        }
                        else
                        {
                            lbStatus.Text = "Hình Upload không hợp lệ, vui lòng chọn hình ảnh khác.";
                            return;
                        }
                    }
                    else if (_id == 0)
                    {
                        lbStatus.Text = "Vui lòng chọn hình ảnh đại diện.";
                        return;
                    }
                    else fileName1 = HiddenField1.Value;
                }
                catch
                { }
                infoNew.ImageThumb = fileName1;
                #endregion

                #region Upload Image Slide
                string fileName2 = string.Empty;
                string filePath2 = Server.MapPath(string.Format("~/Uploads/News/Slide/"));
                try
                {
                    if (!Directory.Exists(filePath2))
                    {
                        Directory.CreateDirectory(filePath2);
                    }
                    if (FileUpload2.HasFile)
                    {
                        if (cvk.AppCode.WebUtility.IsValidFile(FileUpload2.FileName, ".jpg", ".jpeg", ".gif", ".JPG", ".JPEG", ".GIF", ".bmp", ".Bmp", ".BMP") && FileUpload2.FileContent.Length <= 3145728) //3M
                        {
                            fileName2 = cvk.AppCode.WebUtility.GetDateTimeString(FileUpload2.FileName);
                            cvk.AppCode.WebUtility.UploadImage(FileUpload2, filePath2, fileName2, 550, 331);
                            //FileUpload2.SaveAs(filePath2 + fileName2);
                        }
                        else
                        {
                            lbStatus.Text = "hình Upload không hợp lệ, vui lòng chọn hình ảnh khác.";
                            return;
                        }
                    }
                    //else if (_id == 0)
                    //{
                    //    lbStatus.Text = "Vui lòng chọn hình ảnh đại diện.";
                    //    return;
                    //}
                    else fileName2 = HiddenField2.Value;
                }
                catch
                { }
                infoNew.ImageSlide = fileName2;
                #endregion
                #endregion

                #region Save Database
                if (_id > 0)
                {
                    NewsBll.claNews.Update(infoNew, Utility.strConnect);
                    NewsBll.claNewsDetail.Update(info, Utility.strConnect);
                    lbStatus.Text = "Quá trình cập nhật thành công.";
                }
                else
                {
                    int id = NewsBll.claNews.Insert(infoNew, Utility.strConnect);
                    //Insert News_NewsType
                    Cvk_News_NewsType infoNNT = new Cvk_News_NewsType();
                    infoNNT.NewsId = id;
                    infoNNT.NewsTypeId = Convert.ToInt32(ddlNewsType.SelectedValue);
                    NewsBll.claNews_NewsType.Insert(infoNNT, Utility.strConnect);
                    info.NewsId = id;
                    if (NewsBll.claNewsDetail.Insert(info, Utility.strConnect))
                    {
                        lbStatus.Text = "Quá trình thêm mới thành công.";
                        Reset();
                    }
                    else lbStatus.Text = "Quá trình thêm mới Thất bại.";
                }
                //Reset();
                #endregion
            }
            catch (Exception ex)
            {
                lbStatus.Text = ex.Message;
            }
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            Reset();
        }
        public void Reset()
        {
            txtSubject.Text
                = txtNote.Text
                = fckContent.Value
                = txtSEODescription.Text
                = txtSEOKeyword.Text
                = txtSEOTitle.Text
                //= txtSEOImageAlt.Text
                //= txtSEOImageTitle.Text
                = txtSEOUrl.Text = string.Empty;
            txtLinks.Text = string.Empty;
            _id = 0;
            btnUpdate.Text = "Thêm mới";
        }
    }
}
